<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="80px">
      <el-form-item label="类型编码" prop="typeCode">
        <el-select
          v-model="queryParams.typeCode"
          placeholder="请选择设备类型"
          clearable
          style="width: 150px"
        >
          <el-option label="摄像头" value="camera" />
          <el-option label="传感器" value="sensor" />
          <el-option label="投料机" value="feeder" />
          <el-option label="增氧机" value="aerator" />
          <el-option label="水泵" value="pump" />
          <el-option label="灯光" value="light" />
        </el-select>
      </el-form-item>
      <el-form-item label="类型名称" prop="typeName">
        <el-input
          v-model="queryParams.typeName"
          placeholder="请输入类型名称"
          clearable
          style="width: 150px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="启用状态" prop="isActive">
        <el-select
          v-model="queryParams.isActive"
          placeholder="请选择状态"
          clearable
          style="width: 120px"
        >
          <el-option label="启用" value="1" />
          <el-option label="禁用" value="0" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:deviceType:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:deviceType:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:deviceType:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:deviceType:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table 
      v-loading="loading" 
      :data="deviceTypeList" 
      @selection-change="handleSelectionChange"
      stripe
      border
      highlight-current-row
      class="common-table"
    >
      <el-table-column type="selection" width="50" align="center" />
      <el-table-column label="ID" align="center" prop="typeId" width="80" />
      <el-table-column label="类型编码" align="center" prop="typeCode" width="100">
        <template slot-scope="scope">
          <el-tag :type="getTypeCodeTagType(scope.row.typeCode)" size="small">
            {{ getTypeCodeLabel(scope.row.typeCode) }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="类型名称" align="center" prop="typeName" width="120" />
      <el-table-column label="图标" align="center" prop="typeIcon" width="120">
        <template slot-scope="scope">
          <i :class="scope.row.typeIcon" style="font-size: 16px; color: #409EFF"></i>
          <span style="margin-left: 8px; font-size: 12px; color: #666;">{{ scope.row.typeIcon }}</span>
        </template>
      </el-table-column>
      <el-table-column label="描述" align="left" prop="description" min-width="200" show-overflow-tooltip />
      <el-table-column label="启用状态" align="center" prop="isActive" width="100">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.isActive"
            active-value="1"
            inactive-value="0"
            @change="handleStatusChange(scope.row)"
            :disabled="!checkPermi(['system:deviceType:edit'])"
          >
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="140" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:deviceType:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:deviceType:remove']"
            style="color: #F56C6C"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改设备类型对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="类型编码" prop="typeCode">
              <el-select v-model="form.typeCode" placeholder="请选择设备类型" style="width: 100%">
                <el-option label="摄像头" value="camera" />
                <el-option label="传感器" value="sensor" />
                <el-option label="投料机" value="feeder" />
                <el-option label="增氧机" value="aerator" />
                <el-option label="水泵" value="pump" />
                <el-option label="灯光" value="light" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="类型名称" prop="typeName">
              <el-input v-model="form.typeName" placeholder="请输入类型名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="图标样式" prop="typeIcon">
              <el-select v-model="form.typeIcon" placeholder="请选择图标" style="width: 100%">
                <el-option value="el-icon-video-camera">
                  <i class="el-icon-video-camera"></i> 视频摄像
                </el-option>
                <el-option value="el-icon-connection">
                  <i class="el-icon-connection"></i> 传感器
                </el-option>
                <el-option value="el-icon-s-goods">
                  <i class="el-icon-s-goods"></i> 投料机
                </el-option>
                <el-option value="el-icon-setting">
                  <i class="el-icon-setting"></i> 增氧机
                </el-option>
                <el-option value="el-icon-cpu">
                  <i class="el-icon-cpu"></i> 水泵
                </el-option>
                <el-option value="el-icon-sunny">
                  <i class="el-icon-sunny"></i> 灯光
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="启用状态" prop="isActive">
              <el-switch
                v-model="form.isActive"
                active-value="1"
                inactive-value="0"
                active-text="启用"
                inactive-text="禁用"
              >
              </el-switch>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="类型描述" prop="description">
              <el-input 
                v-model="form.description" 
                type="textarea" 
                placeholder="请输入设备类型描述信息"
                :rows="3"
                maxlength="500"
                show-word-limit
              />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listDeviceType, getDeviceType, delDeviceType, addDeviceType, updateDeviceType } from "@/api/system/deviceType";

export default {
  name: "DeviceType",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 设备类型表格数据
      deviceTypeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        typeCode: null,
        typeName: null,
        typeIcon: null,
        description: null,
        isActive: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        typeCode: [
          { required: true, message: "设备类型编码不能为空", trigger: "change" }
        ],
        typeName: [
          { required: true, message: "设备类型名称不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询设备类型列表 */
    getList() {
      this.loading = true;
      listDeviceType(this.queryParams).then(response => {
        this.deviceTypeList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 获取设备类型编码标签 */
    getTypeCodeLabel(typeCode) {
      const typeMap = {
        'camera': '摄像头',
        'sensor': '传感器', 
        'feeder': '投料机',
        'aerator': '增氧机',
        'pump': '水泵',
        'light': '灯光'
      };
      return typeMap[typeCode] || typeCode;
    },
    /** 获取设备类型编码标签类型 */
    getTypeCodeTagType(typeCode) {
      const typeMap = {
        'camera': 'primary',
        'sensor': 'success',
        'feeder': 'warning',
        'aerator': 'info',
        'pump': 'danger',
        'light': ''
      };
      return typeMap[typeCode] || '';
    },
    /** 处理状态变更 */
    handleStatusChange(row) {
      let text = row.isActive === "1" ? "启用" : "禁用";
      this.$modal.confirm('确认要"' + text + '""' + row.typeName + '"设备类型吗？').then(function() {
        return updateDeviceType(row);
      }).then(() => {
        this.$modal.msgSuccess(text + "成功");
      }).catch(function() {
        row.isActive = row.isActive === "0" ? "1" : "0";
      });
    },
    /** 权限检查 */
    checkPermi(permissions) {
      return this.$auth.hasPermi(permissions);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        typeId: null,
        typeCode: null,
        typeName: null,
        typeIcon: null,
        description: null,
        isActive: "1",
        createTime: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.typeId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加设备类型";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const typeId = row.typeId || this.ids
      getDeviceType(typeId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改设备类型";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.typeId != null) {
            updateDeviceType(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDeviceType(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const typeIds = row.typeId || this.ids;
      this.$modal.confirm('是否确认删除设备类型编号为"' + typeIds + '"的数据项？').then(function() {
        return delDeviceType(typeIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/deviceType/export', {
        ...this.queryParams
      }, `deviceType_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>

<style scoped>
.common-table {
  margin-top: 10px;
}

.common-table .el-table__header th {
  background-color: #fafafa;
  color: #606266;
  font-weight: 500;
}

.el-tag {
  font-weight: 500;
}

.el-icon-video-camera, .el-icon-connection, .el-icon-s-goods,
.el-icon-setting, .el-icon-cpu, .el-icon-sunny {
  margin-right: 5px;
}

.dialog-footer {
  text-align: right;
  padding-top: 20px;
}

.el-form .el-form-item__label {
  font-weight: 500;
  color: #606266;
}

.el-select .el-input__inner {
  cursor: pointer;
}

.el-switch {
  vertical-align: middle;
}

/* 搜索表单优化 */
.el-form--inline .el-form-item {
  margin-bottom: 10px;
}

/* 表格操作按钮间距 */
.el-table .cell .el-button + .el-button {
  margin-left: 5px;
}
</style>
